package com.android.gtravelling;

import java.util.ArrayList;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.android.dto.HotelsDTO;
import com.android.dto.ProvincesDTO;

//import com.android.dto.ProvincesDTO;

import android.util.Log;

public class WebserviceCaller {
	private final String NAMESPACE = "http://internship.org/";
	// private final String URL = "http://10.0.2.2:50761/Service1.asmx?WSDL";
	private final String URL = "http://10.0.2.2:50383/GTravellingService.asmx?WSDL";

	public boolean connectSQL() {
		boolean res = false;

		final String SOAP_ACTION = "http://hoangminhuit.org/ConnectionSQL";
		final String METHOD_NAME = "ConnectionSQL";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true; // put this only if the web service is .NET one
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			Log.i("myApp", response.toString());
			if (response.toString().equalsIgnoreCase("true")) {
				res = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return res;
	}

	public String SayHello() {
		String str = "";
		final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
		final String METHOD_NAME = "HelloWorld";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true; // put this only if the web service is .NET one
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			str = response.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
/*
 * Coded by : Xuan ninh
 * get list of  all provinces 
 */
	public ArrayList<ProvincesDTO> selectAllProvinces() {
		ArrayList<ProvincesDTO> lst = new ArrayList<ProvincesDTO>();
		final String SOAP_ACTION = "http://internship.org/GetAllProvince";
		final String METHOD_NAME = "GetAllProvince";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject responseBody = null;

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			for (int i = 0; i < table.getPropertyCount(); i++) {
				ProvincesDTO dto = new ProvincesDTO();
				SoapObject row = (SoapObject) table.getProperty(i);
				dto.setId(Integer.valueOf(row.getProperty("Id").toString()));
				dto.setName(row.getProperty("Name").toString());
				dto.setDescription(row.getProperty("Description").toString());
				dto.setOrder(Integer.valueOf(row.getProperty("Order").toString()));
				lst.add(dto);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}
	//take a database from webserver to Arraylist
		public ArrayList<HotelsDTO> selectAllHotels() {
			ArrayList<HotelsDTO> lst = new ArrayList<HotelsDTO>();
			final String SOAP_ACTION = "http://internship.org/GetAllHotel";
			final String METHOD_NAME = "GetAllHotel";
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

			SoapObject table = null;
			SoapObject responseBody = null;

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try {

				androidHttpTransport.call(SOAP_ACTION, envelope);
				responseBody = (SoapObject) envelope.getResponse();
				
				responseBody = (SoapObject) responseBody.getProperty(1);
				table = (SoapObject) responseBody.getProperty(0);

				for (int i = 0; i < table.getPropertyCount(); i++) {
					HotelsDTO dto = new HotelsDTO();
					SoapObject row = (SoapObject) table.getProperty(i);
					dto.setId(Integer.valueOf(row.getProperty("Id").toString()));
					dto.setName(row.getProperty("Name").toString());
					dto.setAddress(row.getProperty("Address").toString());
					dto.setPhones(row.getProperty("Phones").toString());
					dto.setDescription(row.getProperty("Description").toString());
					
					lst.add(dto);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return lst;
		}
	public ArrayList<Employee> selectAllUsers() {
		String count = "";
		ArrayList<Employee> lst = new ArrayList<Employee>();
		final String SOAP_ACTION = "http://internship.org/GetAllUser";
		final String METHOD_NAME = "GetAllUser";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject client = null;
		SoapObject tableRow = null;
		SoapObject responseBody = null;

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			for (int i = 0; i < table.getPropertyCount(); i++) {
				Employee dto = new Employee();
				String x = "";
				SoapObject row = (SoapObject) table.getProperty(i);
				dto.setName(row.getProperty("name").toString());
				dto.setPhone(row.getProperty("phone").toString());
				dto.setEmail(row.getProperty("email").toString());
				x.trim();
				lst.add(dto);
			}
		} catch (Exception e) {
			e.printStackTrace();
			count = e.toString();
		}
		return lst;
	}

	public Employee selectOneuser(String paccount) {
		Employee emp = new Employee();
		final String SOAP_ACTION = "http://hoangminhuit.org/selectOneuser";
		final String METHOD_NAME = "selectOneuser";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapObject table = null;
		SoapObject client = null;
		SoapObject tableRow = null;
		SoapObject responseBody = null;

		PropertyInfo propInfo1 = new PropertyInfo();
		propInfo1.setName("paccount");
		propInfo1.setValue(paccount);
		propInfo1.setType(String.class);
		request.addProperty(propInfo1);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {

			androidHttpTransport.call(SOAP_ACTION, envelope);
			responseBody = (SoapObject) envelope.getResponse();

			responseBody = (SoapObject) responseBody.getProperty(1);
			table = (SoapObject) responseBody.getProperty(0);

			tableRow = (SoapObject) table.getProperty(0);
			emp.setName(tableRow.getProperty("account").toString());
			emp.setPhone(tableRow.getProperty("phone").toString());
			emp.setEmail(tableRow.getProperty("email").toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return emp;
	}
}